/* Schema dependencies */

var Game = require('../schema/GameSchema').GameSchema,
    Gene = require('../schema/GeneSchema').GeneSchema;

/**
 * Finds all Games that satisfy the given parameters.
 * See API documentation: GET /games/
 *
 * @param req  {HTTP Request}  : HTTP GET request passed from the route
 * @param res  {HTTP Response} : Waiting HTTP response passed from the route
 *
 * @response {JSON Object}     : JSON array of Game objects
 **/

exports.index = function(req, res) {
    var query = Game.find().exclude('_id', 'rounds._id', 
        'rounds.actions.guesses._id', 'rounds.actions.hints._id');

    // Limit query by range
    if (req.query.range) {
        query.limit(req.query.range);
    }

    if (req.query.user) {
        query.where('players').equals(req.query.user);
    }

    if (req.query.not_user) {
        query.where('players').ne(req.query.not_user);
    }

    if (req.query.info) {
        query.where('info').equals(req.query.info);
    }

    // Execute the query
    query.exec(function(err, round) {
        // TODO: JSON error messages
        if (err) { 
            console.log(err);
        } else {
            res.json(round);
        }
    });
};

/**
 * Creates a Game object using the parameters found in the request body.
 * See API documentation: POST /games/
 *
 * @param req  {HTTP Request}  : HTTP POST request passed from the route
 * @param res  {HTTP Response} : Waiting HTTP response passed from the route
 *
 * @response {JSON Object}     : JSON object representing success or error
 **/

exports.create = function(req, res) {
    var newGame = new Game(
        {
            id      : Math.floor(Math.random() * 9000000) + 1000000,
            info  : 'standby',
            players : req.body.players,
            types : {
                hint_type  : req.body.types.hint_type,
                guess_type : req.body.types.guess_type
            },
            identifiers : {
                hint_id  : req.body.identifiers.hint_id,
                guess_id : req.body.identifiers.guess_id
            },
            points : 0,
            rounds : []
        }
    );

    newGame.save(function(err) {
        // TODO: JSON error messages
        if (err) {
            console.log(err);
        } else {
            var returnBody = {
                success : true,
                id      : newGame.id,
                code    : 100,
                message : "Game successfully added to the database."
            };
            res.json(returnBody);
        }
    });
};

/**
 * Finds a single game object with the given game ID.
 * See API documentation: GET /games/[id]
 *
 * @param req  {HTTP Request}  : HTTP GET request passed from the route
 * @param res  {HTTP Response} : Waiting HTTP response passed from the route
 *
 * @response {JSON Object}     : JSON object representing success or error
 **/

exports.show = function(req, res) {
    var query = Game.findOne({id: req.params.game}).exclude('_id');

    query.exec(function(err, game) {
        // TODO: JSON error messages
        if (err) {
            console.log(err);
        } else {
            res.json(game);
        }
    });
};

/**
 * Updates a single Game object with the given game ID.
 * Accepts partial representations of the Game object.
 * See API documentation: PUT /games/[id]
 *
 * @param req  {HTTP Request}  : HTTP PUT request passed from the route
 * @param res  {HTTP Response} : Waiting HTTP response passed from the route
 *
 * @response {JSON Object}     : JSON object representing success or error.
 **/

exports.update = function(req, res) {
    var updatedDoc = {};

    if (req.body.points) {
        updatedDoc.points = req.body.points;
    }
    if (req.body.player) {
        updatedDoc.players.push(req.body.player);
    }
    if (req.body.info) {
        updatedDoc.info = req.body.info;
    }
    if (req.body.players) {
        updatedDoc.players = req.body.players;
    }
    
    Game.update({id: req.params.game}, updatedDoc, function(err) {
        if (err) {
            // TODO: JSON error messages
            console.log(err);
        } else {
            var returnBody = {
                success : true,
                code    : 200,
                message : "Game successfully updated in the database."
            };
            res.json(returnBody);
        }
    });
};

/**
 * Deletes a single Game object with the given game ID.
 * See API documentation: DELETE /games/[id]
 *
 * @param req  {HTTP Request}  : HTTP DELETE request passed from the route
 * @param res  {HTTP Response} : Waiting HTTP Response passed from the route
 *
 * @response {JSON Object}     : JSON object representing success or error.
 **/

exports.destroy = function(req, res) {
    Game.remove({id: req.params.game}, function(err) {
        if (err) {
            // TODO: JSON error messages
            console.log(err);
        } else {
            var returnBody = {
                success : true,
                code    : 300,
                message : "Game successfully deleted from the database."
            };
            res.json(returnBody);
        }
    });
};
